package com.tanhua.app.controllerservice;


import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.*;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionnaireService {

    @Reference
    private UserSoulInfoApi userSoulInfoApi;
    @Reference
    private QuestionBankApi questionBankApi;
    @Reference
    private OptionsApi optionsApi;
    @Reference
    private SubjectApi subjectApi;
    @Reference
    private ReportApi reportApi;
    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity findQuestionnaire() {

        //1、获取用户等级信息
        UserSoulInfo userSoulInfo = userSoulInfoApi.getSoulInfo(UserHolder.getUser().getId());

        //2、去题库中找题
        List<Questionnaire> questionnaireList = questionBankApi.findAllQuestionnaire();

        //3、根据用户等级设置问卷是否上锁
        List voList = new ArrayList();
        String userLevel = userSoulInfo.getLevel();
        if (CollectionUtil.isNotEmpty(questionnaireList)) {
            switch (userLevel) {
                case "初级":
                    for (Questionnaire questionnaire : questionnaireList) {
                        //只解锁初级题库
                        if ("初级".equals(questionnaire.getLevel())) {
                            questionnaire.setIsLock(0);
                        }
                    }
                    break;
                case "中级":
                    for (Questionnaire questionnaire : questionnaireList) {
                        //不是高级的全部解锁（解锁初级和中级）
                        if (!"高级".equals(questionnaire.getLevel())) {
                            questionnaire.setIsLock(0);
                        }
                    }
                    break;
                case "高级":
                    for (Questionnaire questionnaire : questionnaireList) {
                        //解锁所有题库
                        questionnaire.setIsLock(0);
                    }
            }
        }
        String reportId = userSoulInfo.getReportId();
        //4、将题库返回前端
        for (Questionnaire qn : questionnaireList) {

            QuestionnaireVo questionnaireVo = new QuestionnaireVo();
            BeanUtils.copyProperties(qn,questionnaireVo);
            questionnaireVo.setId(qn.getQuestionnaireId());
            questionnaireVo.setQuestions(getSubject(qn.getQuestionId()));
            if (reportId != null){
                questionnaireVo.setReportId(reportId);
            }
            voList.add(questionnaireVo);
        }

        return ResponseEntity.ok(voList);
    }

    //获取试题
    private List getSubject(String questionId){


        //根据试题编号获取试题
        List<Subject> subjectList = subjectApi.getSubjectByQuestionId(questionId);
        List<SubjectVo> subjectVoList = new ArrayList<>();
        //将所有选项封装进试题
        if (CollectionUtil.isNotEmpty(subjectList)){
            for (Subject subject : subjectList) {
                SubjectVo subjectVo = new SubjectVo();
                subjectVo.setId(subject.getTitleId());
                subjectVo.setQuestion(subject.getQuestion());
                subjectVo.setOptions(getOptions(subject.getTitleId()));
                subjectVoList.add(subjectVo);
            }

        }
        return subjectVoList;
    }

    private List<OptionsVo> getOptions(String titleId) {
        //根据小题编号获取对应选项
        List<Options> optionsList= optionsApi.getOptionsByTitleId(titleId);
        List<OptionsVo> optionsVoList = new ArrayList<>();
        for (Options options : optionsList) {
            OptionsVo optionsVo = new OptionsVo();
            optionsVo.setId(options.getOptionId());
            optionsVo.setOption(options.getOption());
            optionsVoList.add(optionsVo);
        }
        return optionsVoList;
    }

    public ResponseEntity findReportId(List list) {
        System.out.println(list.size());
        Integer total = 0;
        for ( Object o: list) {
            String[] strings = o.toString().split(",");
            String[] questionNum = strings[0].split("=");
            String questionId = questionNum[1];
            String[] optionNum = strings[1].split("=");
            String optionId = optionNum[1].substring(0,1);
            Integer score = optionsApi.getScore(questionId,optionId);
            total += score;
        }
        System.out.println("得分："+total);
        String reportId = "4";
        if (total<21){
            reportId = "1";
        }else if (total>=21 && total <= 40){
            reportId = "2";
        }else if (total>=41 && total <= 55){
            reportId = "3";
        }
        return ResponseEntity.ok(reportId);
    }

    public ResponseEntity viewResult(String reportId) {
        //调整用户等级
        UserSoulInfo soulInfo = userSoulInfoApi.getSoulInfo(UserHolder.getUser().getId());
        if ("初级".equals(soulInfo.getLevel())){
            userSoulInfoApi.setLevel(UserHolder.getUser().getId(),"中级");
        }else if ("中级".equals(soulInfo.getLevel())){
            userSoulInfoApi.setLevel(UserHolder.getUser().getId(),"高级");
        }
        //获取报告
        Report report = reportApi.findReportById(reportId);
        //将reportId存入用户信息
        userSoulInfoApi.setReportId(UserHolder.getUser().getId(),reportId);
        //获取维度
        List<Dimensions> dimensionsList = reportApi.findDimendionsById(reportId);
        List<DimensionsVo> dimensionsVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dimensionsList)){
            for (Dimensions dimensions : dimensionsList) {
                DimensionsVo dimensionsVo = new DimensionsVo();
                dimensionsVo.setKey(dimensions.getKey());
                dimensionsVo.setValue(dimensions.getValue());
                dimensionsVoList.add(dimensionsVo);
            }
        }

        //获取相似相似用户
        List<UserSoulInfo> userSoulInfoList = userSoulInfoApi.findSimilar(reportId);
        List<SimilarYouVo> similarYouVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userSoulInfoList)){
            for (UserSoulInfo userSoulInfo : userSoulInfoList) {
                UserInfo userInfo = userInfoApi.findUserInfo(userSoulInfo.getUserId());
                if(UserHolder.getUser().getId().equals(userInfo.getId())){
                    continue;
                }
                SimilarYouVo similarYouVo = new SimilarYouVo();
                similarYouVo.setId(userSoulInfo.getUserId().intValue());
                similarYouVo.setAvatar(userInfo.getAvatar());
                similarYouVoList.add(similarYouVo);
            }
        }
        //返回
        ReportVo reportVo = new ReportVo();
        reportVo.setConclusion(report.getConclusion());
        reportVo.setCover(report.getCover());
        reportVo.setDimensions(dimensionsVoList);
        reportVo.setSimilarYou(similarYouVoList);

        return ResponseEntity.ok(reportVo);
    }
}
